// pages/result/result.js
const qcloud = require('../../vendor/qcloud-weapp-client-sdk/index.js')
const config = require('../../config.js')
const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    questionQuantity:10, //题目数量
    questionRightQuantity: 0, //题目答对数量
    questionScore:10, //一题分数
    score: 0, //得分
    totalScore: 100, //总分
    usedSeconds: 0, //用时
    totalUsedSeconds: 0, //总时间
    imgList: ['Aadd.png', 'A.png', 'B.png', 'C.png', 'D.png', 'E.png'],
    levelIndex:'',
    winRate:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '模拟练习'
    })
    
    if (!!options.lastTime) {
      this.obtainResultLast()
    } else {
      this.createResult()
    }
    
  },
  
  newParamFn(param){
    this.setData({
      questionQuantity: param.questionQuantity
    })

    this.setData({
      totalScore: this.data.questionQuantity * this.data.questionScore
    })

    this.setData({
      questionRightQuantity: param.questionRightQuantity
    })
    
    this.setData({
      usedSeconds: param.usedSeconds
    })

    this.setData({
      totalUsedSeconds: 100
    })

    this.setData({
      levelIndex: this.levelIndexFn(this.data.questionScore * this.data.questionRightQuantity)
    })

    

  },

  obtainResultLast(){
    qcloud.request({
      url: config.service.obtainResultLast,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      login: true,
      data: {
        epId: getApp().globalData.epId
      },
      success: (response) => {
        var data = response.data
        if (data.code === 0) {
          this.newParamFn(data.resultPaperDTO)
        } else {
          wx.showToast({
            title: '系统错误!',
            image: "../../images/error.png",
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function (err) {
        wx.showToast({
          title: '系统错误!',
          image: "../../images/error.png",
          icon: 'none',
          duration: 2000
        })
      }
    });
  },

  createResult(){
    let resultQuestionVOsJn = getApp().globalData.resultQuestionVOsJn

    this.setData({
      questionQuantity: resultQuestionVOsJn.length
    })

    this.setData({
      totalScore: this.data.questionScore * resultQuestionVOsJn.length
    })

    resultQuestionVOsJn.forEach((v, i) => {
      if (!!v.answerCorrect) {
        this.setData({
          questionRightQuantity: this.data.questionRightQuantity + 1
        })
      }

      this.setData({
        usedSeconds: this.data.usedSeconds + v.usedSeconds
      })

      

      this.setData({
        totalUsedSeconds: this.data.totalUsedSeconds + v.limitSeconds
      })

    })

    this.setData({
      levelIndex: this.levelIndexFn(this.data.questionScore * this.data.questionRightQuantity)
    })

    var param = {
      epId: getApp().globalData.epId,
      name: getApp().globalData.name,
      questionQuantity: this.data.questionQuantity,
      questionRightQuantity: this.data.questionRightQuantity,
      resultQuestionVOsJn: JSON.stringify(resultQuestionVOsJn),
      score: this.data.questionScore * this.data.questionRightQuantity,
      'type': 0,
      usedSeconds: this.data.usedSeconds
    }

    qcloud.request({
      url: config.service.createResult,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      login: true,
      data: this.serialize(param),
      success: (response) => {
        var data = response.data

        if (data.code !== 0) {
          wx.showToast({
            title: '系统错误!',
            image: "../../images/error.png",
            icon: 'none',
            duration: 2000
          })
        } else if(data.code === 0 ) {
          this.setData({
            winRate: Math.round( ( 1- data.ranking / data.personTrips) * 100)
          })
        }
      },
      fail: function (err) {
        wx.showToast({
          title: '系统错误!',
          image: "../../images/error.png",
          icon: 'none',
          duration: 2000
        })
      }
    });
  },

  serialize(obj){
    var str = ""
    for (var key in obj) {
      var string = key + '=' + encodeURIComponent(obj[key]) + '&'
      str += string
    }
    str = str.slice(0, -1)
    return str
  },

  levelIndexFn(score){
    if (score == 100) {
      return 0
    } else if (90 <= score && score <= 99) {
      return 1
    } else if (80 <= score && score <= 89) {
      return 2
    } else if (70 <= score && score <= 79) {
      return 3
    } else if (60 <= score && score <= 69) {
      return 4
    } else {
      return 5
    }
  }
})